/**
 * RoleDaoImpl.java
 * Created at 2017-07-06
 * Created by Jackie liu
 * Copyright (C) 2014, All rights reserved.
 */
package com.huabo.dao.impl.oracle;

import com.huabo.base.BaseDaoImpl;
import com.huabo.dao.inter.RoleDao;
import com.huabo.enumeration.YesNoEnum;
import com.huabo.model.Role;
import com.huabo.model.RoleUser;
import com.huabo.model.User;
import com.huabo.sql.builder.SelectBuilder;
import com.huabo.sql.builder.WhereBuilder;
import com.huabo.utils.Page;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * ClassName: RoleDaoImpl
 * Description: 角色管理Dao实现
 * Author: Jackie liu
 * Date: 2017-07-06
 */
@Repository
public class RoleDaoImpl extends BaseDaoImpl<Role, Long> implements RoleDao {

    @Override
    public List<Role> queryByUserId(Long userId) {
        /*String sql = String.format("SELECT R.%s FROM TR_ROLE_USER RU JOIN %s R ON RU.F_ROLE_ID = R.F_ID WHERE R.F_STATE = ? AND RU.F_USER_ID = ?",
                joinColumn(", R."), domainModelAnalysis.getTableName());*/

        WhereBuilder wb = new WhereBuilder();
        wb.andEquals("R.F_STATE", YesNoEnum.YES_ACCPET.getValue());
        wb.andEquals("RU.F_USER_ID", userId);

        SelectBuilder selectBuilder = selectBuilder("R");
        selectBuilder.from(domainModelAnalysis.getTableName()).alias("R").build()
                .join(RoleUser.class).alias("RU").on("RU.F_ROLE_ID = R.F_ID").build();

        return query(selectBuilder.getSql(), wb);
    }

    @Override
    public List<Role> query(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        if (params != null && params.size() > 0) {
            wb.andEquals("F_STATE", params.get("state"));
        }
        return super.query(wb);
    }

    @Override
    public Page<Role> queryPage(Map<String, Object> params) {
        //String sql = "SELECT TR.*, TU.F_NAME AS F_CREATE_NAME FROM TS_ROLE TR JOIN TS_USER TU ON TR.F_CREATE_ID = TU.F_ID";

        SelectBuilder selectBuilder = selectBuilder("TR");
        selectBuilder.column("TU.F_NAME AS F_CREATE_NAME")
                .from(domainModelAnalysis.getTableName()).alias("TR").build()
                .join(User.class).alias("TU").on("TR.F_CREATE_ID = TU.F_ID").build();

        WhereBuilder wb = new WhereBuilder();
        wb.setOrderBy("TR.F_CREATE_TIME DESC");
        if (params != null) {
            wb.andFullLike("TR.F_NAME", params.get("name"));

            initPageParam(wb, params);
        }
        return queryPage(selectBuilder.getSql(), wb);
    }

    @Override
    public int batchRemove(Long[] ids) {
        WhereBuilder wb = new WhereBuilder();
        wb.andIn("_ID", ids);
        return deleteByCondition(wb);
    }

    @Override
    public int modifyState(final Long[] ids, final int state) {
        String sql = String.format("UPDATE %s SET F_STATE=? WHERE F_ID=?", domainModelAnalysis.getTableName());
        int[] batchUpdate = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setInt(1, state);
                ps.setLong(2, ids[i]);
            }

            @Override
            public int getBatchSize() {
                return ids.length;
            }
        });
        return batchUpdate.length;
    }

    @Override
    public Boolean isDuplicateName(Long id, String name) {
        return super.isDuplicateField(id, name, "F_NAME");
    }
}